/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainGui.java
 *
 * Created on Jun 30, 2010, 12:00:02 PM
 */
package proiectpractica;

import java.math.BigDecimal;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

/**
 *
 * @author Claudiu
 */
public class MainGui extends javax.swing.JFrame {

    Connection con = null;
    Statement st = null;
    ResultSet rs = null;
    String topTableName;
    String bottomTableName;

    /** Creates new form MainGui */
    public MainGui(Connection conn) {
        initComponents();

        con = conn;


        try {
            st = conn.createStatement();
            setTopTableToMaterialeHeaders();
            addMaterialeToTopTable();
            label1.setText("Materiale");
            topTableName = "Materiale";
            setBottomTableToStocHeaders();
            addStocToBottomTable();
            label2.setText("Stoc");
            bottomTableName = "Stoc";

        } catch (SQLException ex) {
            Logger.getLogger(MainGui.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }

    }

    public void setBottomTableToStocHeaders() {
        DefaultTableModel aModel = (DefaultTableModel) Table_Bottom.getModel();
        aModel.setColumnCount(3);
        String[] columns = new String[3];
        columns[0] = "idStoc";
        columns[1] = "nume Material";
        columns[2] = "cantitate";

        aModel.setColumnIdentifiers(columns);

    }

    public void addStocToBottomTable() {
        DefaultTableModel aModel = (DefaultTableModel) Table_Bottom.getModel();

        while (aModel.getRowCount() != 0) {
            aModel.removeRow(0);
        }
        try {


            //  aModel.setColumnIdentifiers( jTable1.getColumnModel().getColumns());
            rs = st.executeQuery("select idStoc, m.nume , cantitate from stoc s , materiale m  where s.idMaterial = m.idMaterial");
            java.sql.ResultSetMetaData rsmd = rs.getMetaData();
            int colNo = rsmd.getColumnCount();
            int null_count;
            while (rs.next()) {
                null_count = 0;
                Object[] objects = new Object[colNo];
                // tanks to umit ozkan for the bug fix!
                for (int i = 0; i < colNo; i++) {
                    objects[i] = rs.getObject(i + 1);

                    if (objects[i] == null) {
                        null_count++;
                    }
                }
                //System.out.println();
                if (null_count < 4) {
                    aModel.addRow(objects);
                }

            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
    }

    public void setBottomTableToIntrariHeaders() {
        DefaultTableModel aModel = (DefaultTableModel) Table_Bottom.getModel();
        aModel.setColumnCount(4);
        String[] columns = new String[4];
        columns[0] = "idIntrare";
        columns[1] = "Nume Material";
       // columns[2] = "id stoc ";
        columns[2] = " cantitate ";
        columns[3] = "Data Intrare";
        aModel.setColumnIdentifiers(columns);

    }

    public void setBottomTableToIesiriHeaders() {
        DefaultTableModel aModel = (DefaultTableModel) Table_Bottom.getModel();
        aModel.setColumnCount(3);
        String[] columns = new String[3];
        columns[0] = "idIesire";
        //columns[1] = "id stoc";
        columns[1] = " cantitate ";
        columns[2] = "Data Iesire";
        aModel.setColumnIdentifiers(columns);

    }

    public void setTopTableToMaterialeHeaders() {
        DefaultTableModel aModel = (DefaultTableModel) Table_Top.getModel();
        aModel.setColumnCount(7);
        String[] columns = new String[7];
        columns[0] = "idMaterial";
        columns[1] = "Nume Furnizor";
        columns[2] = "Nume Material";
        columns[3] = "Tip";
        columns[4] = "Descriere";
        columns[5] = "Valabil";
        columns[6] = "Pret";
        aModel.setColumnIdentifiers(columns);

    }

    public void setTopTableToFurnizoriHeaders() {
        DefaultTableModel aModel = (DefaultTableModel) Table_Top.getModel();
        aModel.setColumnCount(4);
        String[] columns = new String[4];
        columns[0] = "idIntrare";
        columns[1] = "Nume Furnizor";
        columns[2] = "addresac ";
        columns[3] = "valabil";

        aModel.setColumnIdentifiers(columns);

    }

    public void addIntrariToBottomTable() {
        DefaultTableModel aModel = (DefaultTableModel) Table_Bottom.getModel();

        while (aModel.getRowCount() != 0) {
            aModel.removeRow(0);
        }
        try {


            //  aModel.setColumnIdentifiers( jTable1.getColumnModel().getColumns());
            rs = st.executeQuery("select idintrare, m.nume , cantitate, dataintrare from intrari s , materiale m  where s.idMaterial = m.idMaterial");
            java.sql.ResultSetMetaData rsmd = rs.getMetaData();
            int colNo = rsmd.getColumnCount();
            int null_count;
            while (rs.next()) {
                null_count = 0;
                Object[] objects = new Object[colNo];
                // tanks to umit ozkan for the bug fix!
                for (int i = 0; i < colNo; i++) {
                    objects[i] = rs.getObject(i + 1);

                    if (objects[i] == null) {
                        null_count++;
                    }
                }
                //System.out.println();
                if (null_count < 5) {
                    aModel.addRow(objects);
                }

            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
    }

    public void addIesiriToBottomTable() {
        DefaultTableModel aModel = (DefaultTableModel) Table_Bottom.getModel();

        while (aModel.getRowCount() != 0) {
            aModel.removeRow(0);
        }
        try {
            //  aModel.setColumnIdentifiers( jTable1.getColumnModel().getColumns());
            rs = st.executeQuery("select idiesire, cantitate, dataiesire from iesiri");
            java.sql.ResultSetMetaData rsmd = rs.getMetaData();
            int colNo = rsmd.getColumnCount();
            int null_count;
            while (rs.next()) {
                null_count = 0;
                Object[] objects = new Object[colNo];
                // tanks to umit ozkan for the bug fix!
                for (int i = 0; i < colNo; i++) {
                    objects[i] = rs.getObject(i + 1);

                    if (objects[i] == null) {
                        null_count++;
                    }
                }
                //System.out.println();
                //if (null_count < 4) {
                aModel.addRow(objects);
            //}

            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
    }

    public void addMaterialeToTopTable() {
        DefaultTableModel aModel = (DefaultTableModel) Table_Top.getModel();

        while (aModel.getRowCount() != 0) {
            aModel.removeRow(0);
        }
        try {


            //  aModel.setColumnIdentifiers( jTable1.getColumnModel().getColumns());
            rs = st.executeQuery("select m.idmaterial, f.nume ,m.nume, tip , descriere ,m.valabil, pret from materiale m , furnizor f  where m.idFurnizor = f.idFurnizor");
            java.sql.ResultSetMetaData rsmd = rs.getMetaData();
            int colNo = rsmd.getColumnCount();
            int null_count;
            while (rs.next()) {
                null_count = 0;
                Object[] objects = new Object[colNo];
                // tanks to umit ozkan for the bug fix!
                for (int i = 0; i < colNo; i++) {
                    objects[i] = rs.getObject(i + 1);

                    if (objects[i] == null) {
                        null_count++;
                    }
                }
                //System.out.println();
                if (null_count < 5) {
                    aModel.addRow(objects);
                }

            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
    }

    public void addFurnizorToTopTable() {
        DefaultTableModel aModel = (DefaultTableModel) Table_Top.getModel();

        while (aModel.getRowCount() != 0) {
            aModel.removeRow(0);
        }
        try {


            //  aModel.setColumnIdentifiers( jTable1.getColumnModel().getColumns());
            rs = st.executeQuery("select idfurnizor,nume , addresa ,valabil from  furnizor ");
            java.sql.ResultSetMetaData rsmd = rs.getMetaData();
            int colNo = rsmd.getColumnCount();
            int null_count;
            while (rs.next()) {
                null_count = 0;
                Object[] objects = new Object[colNo];
                // tanks to umit ozkan for the bug fix!
                for (int i = 0; i < colNo; i++) {
                    objects[i] = rs.getObject(i + 1);

                    if (objects[i] == null) {
                        null_count++;
                    }
                }
                //System.out.println();
                if (null_count < 4) {
                    aModel.addRow(objects);
                }

            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
    }

    public MainGui() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        Table_Top = new javax.swing.JTable();
        label1 = new java.awt.Label();
        jScrollPane2 = new javax.swing.JScrollPane();
        Table_Bottom = new javax.swing.JTable();
        label2 = new java.awt.Label();
        GetOOut = new javax.swing.JButton();
        Refresh = new javax.swing.JButton();
        AddFurnizor = new javax.swing.JButton();
        DeleteMatFurn = new javax.swing.JButton();
        Iesiri = new javax.swing.JButton();
        AddMaterial = new javax.swing.JButton();
        StergeIntrare = new javax.swing.JButton();
        Furnizori = new javax.swing.JButton();
        Materiale = new javax.swing.JButton();
        Stoc = new javax.swing.JButton();
        intrari = new javax.swing.JButton();
        IntroducereMaterial = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        Search = new javax.swing.JButton();
        SearchBox = new javax.swing.JTextField();
        Modifica = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Main Form");
        setBackground(new java.awt.Color(204, 204, 204));

        Table_Top.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "IdMaterial", "IdFrunizor", "Nume", "Tip", "Descriere", "Valabil", "Pret"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        Table_Top.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        Table_Top.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(Table_Top);
        Table_Top.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        Table_Top.getColumnModel().getColumn(0).setResizable(false);
        Table_Top.getColumnModel().getColumn(1).setResizable(false);
        Table_Top.getColumnModel().getColumn(2).setResizable(false);
        Table_Top.getColumnModel().getColumn(3).setResizable(false);
        Table_Top.getColumnModel().getColumn(4).setResizable(false);

        label1.setText("Materiale");

        Table_Bottom.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "IdIntrare", "IdMaterial", "IdStoc", "Cantitate", "DataIntrare"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, true, false, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        Table_Bottom.setColumnSelectionAllowed(true);
        Table_Bottom.getTableHeader().setReorderingAllowed(false);
        jScrollPane2.setViewportView(Table_Bottom);
        Table_Bottom.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        Table_Bottom.getColumnModel().getColumn(0).setResizable(false);
        Table_Bottom.getColumnModel().getColumn(1).setResizable(false);
        Table_Bottom.getColumnModel().getColumn(2).setResizable(false);
        Table_Bottom.getColumnModel().getColumn(3).setResizable(false);
        Table_Bottom.getColumnModel().getColumn(4).setResizable(false);

        label2.setText("Intrari in depozit");

        GetOOut.setText("Exit");
        GetOOut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                GetOOutActionPerformed(evt);
            }
        });

        Refresh.setText("Refresh");
        Refresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RefreshActionPerformed(evt);
            }
        });

        AddFurnizor.setText("Add Furnizor");
        AddFurnizor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddFurnizorActionPerformed(evt);
            }
        });

        DeleteMatFurn.setText("Sterge Material /Furnizor");
        DeleteMatFurn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DeleteMatFurnActionPerformed(evt);
            }
        });

        Iesiri.setText("Iesiri");
        Iesiri.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                IesiriActionPerformed(evt);
            }
        });

        AddMaterial.setText("Add Material");
        AddMaterial.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddMaterialActionPerformed(evt);
            }
        });

        StergeIntrare.setText("Sterge Intrare/Iesire");
        StergeIntrare.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                StergeIntrareActionPerformed(evt);
            }
        });

        Furnizori.setText("Furnizori");
        Furnizori.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FurnizoriActionPerformed(evt);
            }
        });

        Materiale.setText("Materiale");
        Materiale.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MaterialeActionPerformed(evt);
            }
        });

        Stoc.setText("Stoc");
        Stoc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                StocActionPerformed(evt);
            }
        });

        intrari.setText("Intrari");
        intrari.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                intrariActionPerformed(evt);
            }
        });

        IntroducereMaterial.setText("Introdu material in depozit");
        IntroducereMaterial.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                IntroducereMaterialActionPerformed(evt);
            }
        });

        jButton1.setText("Scoate Material din Stoc");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        Search.setText("Cautare Material In Stoc (dupa Nume)");
        Search.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SearchActionPerformed(evt);
            }
        });

        Modifica.setText("Modifica ");
        Modifica.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ModificaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(SearchBox, javax.swing.GroupLayout.DEFAULT_SIZE, 243, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(Search, javax.swing.GroupLayout.PREFERRED_SIZE, 292, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 545, Short.MAX_VALUE)))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(218, 218, 218)
                            .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(205, 205, 205)
                            .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)
                    .addComponent(IntroducereMaterial, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)
                    .addComponent(intrari, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)
                    .addComponent(Stoc, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)
                    .addComponent(StergeIntrare, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)
                    .addComponent(Iesiri, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)
                    .addComponent(GetOOut, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)
                    .addComponent(AddMaterial, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)
                    .addComponent(AddFurnizor, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)
                    .addComponent(Materiale, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)
                    .addComponent(Furnizori, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)
                    .addComponent(Refresh, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)
                    .addComponent(DeleteMatFurn, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)
                    .addComponent(Modifica, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(GetOOut)
                        .addGap(7, 7, 7)
                        .addComponent(Furnizori)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(Materiale)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(AddFurnizor)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(AddMaterial)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(Modifica)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(DeleteMatFurn))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(Refresh)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(Stoc)
                        .addGap(11, 11, 11)
                        .addComponent(intrari)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(IntroducereMaterial)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(StergeIntrare)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Iesiri)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Search)
                    .addComponent(SearchBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void GetOOutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_GetOOutActionPerformed
        // TODO add your handling code here:
        this.dispose();


}//GEN-LAST:event_GetOOutActionPerformed

    private void RefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RefreshActionPerformed
        // TODO add your handling code here:
        //for top table
        if (topTableName.equalsIgnoreCase("Materiale")) {
            setTopTableToMaterialeHeaders();
            addMaterialeToTopTable();
            label1.setText("Materiale");
            topTableName = "Materiale";
        }
        if (topTableName.equalsIgnoreCase("Furnizori")) {
            setTopTableToFurnizoriHeaders();
            addFurnizorToTopTable();
            label1.setText("Furnizori");
            topTableName = "Furnizori";
        }
        if (bottomTableName.equalsIgnoreCase("Stoc")) {
            setBottomTableToStocHeaders();
            addStocToBottomTable();
            label2.setText("Stoc");
            bottomTableName = "Stoc";
        }
        if (bottomTableName.equalsIgnoreCase("Intrari")) {
            setBottomTableToIntrariHeaders();
            addIntrariToBottomTable();
            label2.setText("Intrari");
            bottomTableName = "Intrari";
        }
        if (bottomTableName.equalsIgnoreCase("Iesiri")) {
            setBottomTableToIesiriHeaders();
            addIesiriToBottomTable();
            label2.setText("Iesiri");
            bottomTableName = "Iesiri";
        }

    }//GEN-LAST:event_RefreshActionPerformed

    private void AddFurnizorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddFurnizorActionPerformed
        // TODO add your handling code here:
        Add_Furnizor ac = new Add_Furnizor(con);
        ac.setVisible(true);
        RefreshActionPerformed(evt);
}//GEN-LAST:event_AddFurnizorActionPerformed

    private void AddMaterialActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddMaterialActionPerformed
        // TODO add your handling code here:
        Add_Material al = new Add_Material(con);
        al.setVisible(true);
        RefreshActionPerformed(evt);
}//GEN-LAST:event_AddMaterialActionPerformed

    private void DeleteMaterial(int id) {
        JOptionPane.showMessageDialog(null, "Atentie! \n Stergerea unui material este definitiva!");

        int choice = JOptionPane.showConfirmDialog(null, "Sunteti sigur ca doriti sa stergeti materialul cu index " + id + " ?");

        if (choice == 0) {
            try {
                CallableStatement cs = con.prepareCall("{call deletematerialp(?)}");
                cs.setInt(1, id);
                cs.execute();
                JOptionPane.showMessageDialog(null, "Cererea de Stergere a materialului cu nr "+id+" a fost executata cu succes pe  server!");
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e.getMessage());
            }
        }
    }

    private void DeleteFurnizor(int id) {
        JOptionPane.showMessageDialog(null, "Atentie! \n Stergerea unui furnizor este definitiva !");

        int choice = JOptionPane.showConfirmDialog(null, "Sunteti sigur ca doriti sa stergeti furnizorului cu index " + id + " ?");
        if (choice == 0) {
            try {
                CallableStatement cs = con.prepareCall("{call DELFURNIZORproc(?)}");
                cs.setInt(1, id);
                cs.execute();
                JOptionPane.showMessageDialog(null, "Cererea de Stergere a furnizorului cu nr "+id+" a fost executata cu succes pe  server!");
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e.getMessage());
            }
        }
    }

    private void DeleteIntrari(int id) {
        JOptionPane.showMessageDialog(null, "Atentie! \n Stergerea unei intrari este definitiva !");

        int choice = JOptionPane.showConfirmDialog(null, "Sunteti sigur ca doriti sa stergeti Intarea cu index " + id + " ?");
        if (choice == 0) {
            try {
                CallableStatement cs = con.prepareCall("{call deleteIntrare(?)}");
                cs.setInt(1, id);
                cs.execute();
                JOptionPane.showMessageDialog(null, "Cererea de Stergere a intrari cu nr "+id+" a fost executata cu succes pe  server!");
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e.getMessage());
            }
        }
    }

    private void DeleteIesire(int id) {
        JOptionPane.showMessageDialog(null, "Atentie! \n Stergerea unei Iesiri este definitiva !");

        int choice = JOptionPane.showConfirmDialog(null, "Sunteti sigur ca doriti sa stergeti Iesirea cu index " + id + " ?");
        if (choice == 0) {
            try {
                CallableStatement cs = con.prepareCall("{call deleteIesire(?)}");
                cs.setInt(1, id);
                cs.execute();
                JOptionPane.showMessageDialog(null, "Cererea de Stergere a iesirii cu nr "+id+" a fost executata cu succes pe  server!");
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e.getMessage());
            }
        }
    }
    private void DeleteMatFurnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DeleteMatFurnActionPerformed
        // TODO add your handling code here:
        int selected_index;
        selected_index = Table_Top.getSelectedRow();

        if (selected_index < 0) {
            JOptionPane.showMessageDialog(null, "Selectati o intrare!");
        } else {
            BigDecimal aux1 = (BigDecimal) Table_Top.getValueAt(selected_index, 0);
            System.out.println(aux1);
            // int aux= (Integer) Table_Top.getValueAt(selected_index, 0);
            int aux = aux1.intValue();

            try {
                if (topTableName.equalsIgnoreCase("Materiale")) {
                    DeleteMaterial(aux);
                } else if (topTableName.equalsIgnoreCase("Furnizori")) {
                    DeleteFurnizor(aux);
                }
            } catch (Exception ex) {
                Logger.getLogger(MainGui.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(null, ex.getMessage());
            }
        }
        RefreshActionPerformed(evt);
}//GEN-LAST:event_DeleteMatFurnActionPerformed

    private void StergeIntrareActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_StergeIntrareActionPerformed
        // TODO add your handling code here:
        //     int a = JOptionPane.showConfirmDialog(null, " test message");
        //    System.out.println("   ->" + a);
        JOptionPane.showMessageDialog(null, "Conform regulamentului fiscal este ilegala stergerea operatiunilor din evidenta. \n Pentru fals si uz de fals se primesc intre 6 luni si 3 ani. \n Va rugam sa va asigurati ca ati dat spaga.");
        int selected_index;
        selected_index = Table_Bottom.getSelectedRow();
        if (selected_index < 0) {
            JOptionPane.showMessageDialog(null, "Selectati o intrare!");
        } else {
            BigDecimal aux1 = (BigDecimal) Table_Bottom.getValueAt(selected_index, 0);
            System.out.println(aux1);
            // int aux= (Integer) Table_Top.getValueAt(selected_index, 0);
            int aux = aux1.intValue();


            try {
                if (bottomTableName.equalsIgnoreCase("Intrari")) {
                    //
                    DeleteIntrari(aux);
                } else if (bottomTableName.equalsIgnoreCase("Iesiri")) {
                    //
                    DeleteIesire(aux);
                } else {
                    JOptionPane.showMessageDialog(null, "Se pot face stergeri doar din intrari si iesiri , nu si din stoc");
                }
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            //  Logger.getLogger(MainGui.class.getName()).log(Level.SEVERE, null, ex);

            }
        }
        RefreshActionPerformed(evt);
    //        int selected_index;
//        selected_index= Table_Bottom.getSelectedRow();
//        try {
//            st.executeUpdate("delete from scott.livrari where cod_livrare = " + Table_Bottom.getValueAt(selected_index, 0));
//        } catch (SQLException ex) {
//            Logger.getLogger(MainGui.class.getName()).log(Level.SEVERE, null, ex);
//        }
//                RefreshActionPerformed(evt);
}//GEN-LAST:event_StergeIntrareActionPerformed

    private void StocActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_StocActionPerformed
        // TODO add your handling code here:
        setBottomTableToStocHeaders();
        addStocToBottomTable();
        label2.setText("Stoc");
        bottomTableName = "Stoc";
}//GEN-LAST:event_StocActionPerformed

    private void intrariActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_intrariActionPerformed
        // TODO add your handling code here:
        setBottomTableToIntrariHeaders();
        addIntrariToBottomTable();
        label2.setText("Intrari");
        bottomTableName = "Intrari";
}//GEN-LAST:event_intrariActionPerformed

    private void FurnizoriActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FurnizoriActionPerformed
        // TODO add your handling code here:
        setTopTableToFurnizoriHeaders();
        addFurnizorToTopTable();
        label1.setText("Furnizori");
        topTableName = "Furnizori";
}//GEN-LAST:event_FurnizoriActionPerformed

    private void MaterialeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MaterialeActionPerformed
        // TODO add your handling code here:
        setTopTableToMaterialeHeaders();
        addMaterialeToTopTable();
        label1.setText("Materiale");
        topTableName = "Materiale";
}//GEN-LAST:event_MaterialeActionPerformed

    private void IntroducereMaterialActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_IntroducereMaterialActionPerformed
        // TODO add your handling code here:
        Add_Intrare ac = new Add_Intrare(con);
        ac.setVisible(true);
        RefreshActionPerformed(evt);
}//GEN-LAST:event_IntroducereMaterialActionPerformed

    private void IesiriActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_IesiriActionPerformed
        // TODO add your handling code here:
        setBottomTableToIesiriHeaders();
        addIesiriToBottomTable();
        label2.setText("Iesiri");
        bottomTableName = "Iesiri";
    }//GEN-LAST:event_IesiriActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        Add_Iesire ac = new Add_Iesire(con);
        ac.setVisible(true);
        RefreshActionPerformed(evt);

    }//GEN-LAST:event_jButton1ActionPerformed

        public void addStocToBottomTable_searchResult(String nume) {
        DefaultTableModel aModel = (DefaultTableModel) Table_Bottom.getModel();

        while (aModel.getRowCount() != 0) {
            aModel.removeRow(0);
        }
        try {


            //  aModel.setColumnIdentifiers( jTable1.getColumnModel().getColumns());
            rs = st.executeQuery("select idStoc, m.nume , cantitate from stoc s , materiale m  where s.idMaterial = m.idMaterial and m.nume = '"+nume+"'");
            java.sql.ResultSetMetaData rsmd = rs.getMetaData();
            int colNo = rsmd.getColumnCount();
            int null_count;
            while (rs.next()) {
                null_count = 0;
                Object[] objects = new Object[colNo];
                // tanks to umit ozkan for the bug fix!
                for (int i = 0; i < colNo; i++) {
                    objects[i] = rs.getObject(i + 1);

                    if (objects[i] == null) {
                        null_count++;
                    }
                }
                //System.out.println();
                if (null_count < 4) {
                    aModel.addRow(objects);
                }

            }
            JOptionPane.showMessageDialog(null, "Au fost gasite "+aModel.getRowCount()+"   intrari in stoc cu numele  materialului : " +nume);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
    }
    private void SearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SearchActionPerformed
        // TODO add your handling code here:
       setBottomTableToStocHeaders();
        addStocToBottomTable_searchResult(SearchBox.getText());
        label2.setText("Stoc");
        bottomTableName = "Stoc";
    }//GEN-LAST:event_SearchActionPerformed

    private void ModificaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ModificaActionPerformed
        // TODO add your handling code here:
        if (topTableName .equalsIgnoreCase("Furnizori"))
        {
            int selected_index;
        selected_index = Table_Top.getSelectedRow();

        if (selected_index < 0) {
            JOptionPane.showMessageDialog(null, "Selectati o intrare!");
        } else {
            BigDecimal aux1 = (BigDecimal) Table_Top.getValueAt(selected_index, 0);
            System.out.println(aux1);
            // int aux= (Integer) Table_Top.getValueAt(selected_index, 0);
            int aux = aux1.intValue();
            ModifyFurnizor md = new ModifyFurnizor(con, (String)Table_Top.getValueAt(selected_index, 1), (String)Table_Top.getValueAt(selected_index, 2), aux);
            md.setVisible(true);
        }
        }
    }//GEN-LAST:event_ModificaActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MainGui().setVisible(true);
            }
        });
    }

    public static void run(final Connection con) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MainGui(con).setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddFurnizor;
    private javax.swing.JButton AddMaterial;
    private javax.swing.JButton DeleteMatFurn;
    private javax.swing.JButton Furnizori;
    private javax.swing.JButton GetOOut;
    private javax.swing.JButton Iesiri;
    private javax.swing.JButton IntroducereMaterial;
    private javax.swing.JButton Materiale;
    private javax.swing.JButton Modifica;
    private javax.swing.JButton Refresh;
    private javax.swing.JButton Search;
    private javax.swing.JTextField SearchBox;
    private javax.swing.JButton StergeIntrare;
    private javax.swing.JButton Stoc;
    private javax.swing.JTable Table_Bottom;
    private javax.swing.JTable Table_Top;
    private javax.swing.JButton intrari;
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private java.awt.Label label1;
    private java.awt.Label label2;
    // End of variables declaration//GEN-END:variables
}
